﻿using OpenMms.Common.Log;
using OpenMms.Domain.Interface;
using OpenMms.Domain.Interface.User;
using OpenMms.Domain.Models;
using OpenMms.Domain.ReponseModels;
using OpenMms.Domain.RequestModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace OpenMms.Domain.BLL.User
{
    /// <summary>
    /// 服务中心
    /// </summary>
    public class ServiceCenterService: IServiceCenterService
    {
        #region 初始化
        private IBLLConstruct<ServiceCentre> _repository;
        private IBLLConstruct<UserInforMations> _user_repository;
        public ServiceCenterService(IBLLConstruct<ServiceCentre> repository, 
            IBLLConstruct<UserInforMations> user_repository )
        {
            this._repository = repository;
            this._user_repository = user_repository;
        }
        #endregion

        #region 申请
        /// <summary>
        /// 申请
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Response> Apply(ServiceCenterReq model)
        {
            Response resp = new Response();
            resp.Code = 200;
            resp.Message = "申请成功!";
            if (!await RepeatLock(model))
            {
                resp.Code = 201;
                resp.Message = "重复提交!";
                return resp;
            }
            MyLog.Log(model, "User", "申请服务中心");
            //事务
            if (!await Commit(model))
            {
                resp.Code = 203;
                resp.Message = "事务提交失败!";
                return resp;
            }
            return resp;
        }
        #endregion

        #region 提交事务
        /// <summary>
        /// 提交事务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private async Task<bool> Commit(ServiceCenterReq model)
        {
            //提交事务
            var tran = _repository.BeginTrans();
            try
            {
                await DoApply(model);
                bool result = await ModifyUser(model);
                await _repository.SaveChangesAsync();
                if (!result)
                {
                    tran.Rollback();
                    return false;
                }
                tran.Commit();
                return true;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw ex;
            }
        }
        #endregion

        #region 判断是否重复提交
        /// <summary>
        /// 判断是否重复提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private async Task<bool> RepeatLock(ServiceCenterReq model)
        {
         return await _repository.CountAsync(s => s.ServiceCenterCode == model.ServiceCenterCode)==0;
        }
        #endregion

        #region 写入服务中心表
        /// <summary>
        /// 写入服务中心表
        /// </summary>
        /// <param name="centerReq"></param>
        /// <returns></returns>
        private async Task DoApply( ServiceCenterReq centerReq)
        {
            Models.ServiceCentre model = new Models.ServiceCentre()
            {
                Address = centerReq.Address,
                Level = centerReq.Level,
                Name = centerReq.Name,
                ServiceCenterCode = centerReq.ServiceCenterCode
            };
           await _repository.InsertAsync(model);
        }
        #endregion

        #region 修改会员为待审核
        /// <summary>
        /// 修改会员为待审核
        /// </summary>
        /// <param name="centerReq"></param>
        /// <returns></returns>
        private async Task<bool> ModifyUser(ServiceCenterReq centerReq)
        {
           return await _user_repository.UpdateAsync(s => s.UserCode == centerReq.ServiceCenterCode && s.IsCenterService == 0,
               s=> new Models.UserInforMations() {
                    IsCenterService = 2
                })>0;
        }
        #endregion
    }
}
